package own.stu.jobgib.playown.alg.leetcode.editor.cn.demo;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class LargeFileComparator {

    public static int[] getInts2(String path){
        try {
            // 读取文件
            BufferedReader reader = new BufferedReader(new FileReader(path));
            String line = reader.readLine();
            reader.close();

            // 转换为数组
            int[] nums = parseArray(line);

//            System.out.println("转换结果: " + Arrays.toString(nums));
            return nums;
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public static int[] parseArray(String str) {
        // 去除方括号和空格
        String cleaned = str.replace("[", "").replace("]", "").replace(" ", "");

        // 按逗号分割
        String[] parts = cleaned.split(",");

        // 转换为int数组
        int[] result = new int[parts.length];
        for (int i = 0; i < parts.length; i++) {
            result[i] = Integer.parseInt(parts[i]);
        }

        return result;
    }

    public static int[] getInts(String path){
        try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
            String line;
            List<Integer> numbers = new ArrayList<>();
            while ((line = reader.readLine()) != null) {
                numbers.add(Integer.parseInt(line));
            }
            int[] arr = new int[numbers.size()];
            for (int i = 0; i < numbers.size(); i++) {
                arr[i] = numbers.get(i);
            }
            return arr;
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static List<Difference> compareFiles(String filePath1, String filePath2) throws IOException {
        List<Difference> differences = new ArrayList<>();
        
        try (BufferedReader reader1 = new BufferedReader(new FileReader(filePath1));
             BufferedReader reader2 = new BufferedReader(new FileReader(filePath2))) {
            
            int lineNumber = 1;
            int charPosition = 0;
            String line1, line2;
            
            while ((line1 = reader1.readLine()) != null | (line2 = reader2.readLine()) != null) {
                // 如果其中一个文件已经结束，但另一个还有内容
                if (line1 == null) line1 = "";
                if (line2 == null) line2 = "";
                
                // 比较当前行的字符
                int maxLength = Math.max(line1.length(), line2.length());
                for (int i = 0; i < maxLength; i++) {
                    char char1 = (i < line1.length()) ? line1.charAt(i) : '\0';
                    char char2 = (i < line2.length()) ? line2.charAt(i) : '\0';
                    
                    if (char1 != char2) {
                        differences.add(new Difference(lineNumber, charPosition + i + 1, char1, char2));
                    }
                }
                
                lineNumber++;
                charPosition += maxLength + 1; // +1 用于换行符
            }
        }
        
        return differences;
    }
    
    public static class Difference {
        private int lineNumber;
        private int position;
        private char expected;
        private char actual;
        
        public Difference(int lineNumber, int position, char expected, char actual) {
            this.lineNumber = lineNumber;
            this.position = position;
            this.expected = expected;
            this.actual = actual;
        }
        
        // getters
        public int getLineNumber() { return lineNumber; }
        public int getPosition() { return position; }
        public char getExpected() { return expected; }
        public char getActual() { return actual; }
        
        @Override
        public String toString() {
            return String.format("Line %d, Position %d: expected '%c' but found '%c'", 
                               lineNumber, position, expected, actual);
        }
    }
    
    public static void main(String[] args) {
        try {
            List<Difference> diffs = compareFiles("/Users/qim/Documents/idea-code/play-own/src/main/resources/xxx/1.txt", "file2.txt");
            
            if (diffs.isEmpty()) {
                System.out.println("文件内容完全相同");
            } else {
                System.out.println("找到 " + diffs.size() + " 处差异：");
                for (Difference diff : diffs) {
                    System.out.println(diff);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}